import cherrypy
import ldap

class Auth:
    def __init__(self, session):
        self.sess = session

        self.session_key = 'GIUopkp98987YHUIOKLI*^&*'
        self.admin = '$%^&*(0-%^&890-*^&*'
        self.users = {
            'nikolay.yakovlev@gameloft.com':'',
            'konstantin.teslenko@gameloft.com':'',
            'andrey.sidorov@gameloft.com':'',
            'alexandr.soor@gameloft.com':'',
            'boris.ivakha@gameloft.com':'',
            'boris.kozar@gameloft.com':'',
            'denis.belyaev@gameloft.com':'',
            'roman.yatsenko@gameloft.com':'',
            'roman.drovikov@gameloft.com':'',
            'vitaliy.shpotov@gameloft.com':'',
            'stanislav.podgorskiy@gameloft.com':'',
            'vadim.kravchenko@gameloft.com':'',
            'alexey.nemets@gameloft.com':'',
            'yuriy.boltyan@gameloft.com':'',
            'bogdan.klochko@gameloft.com':'',
            # 'vegasq': '535517',
            # 'vadim': '492475',
            # 'test1':'1111',
            # 'test2':'1111',
            # 'test3':'1111',
            # 'gameloftQA1@gameloft.com':'1111',
            # 'gameloftQA2@gameloft.com':'1111',
            # 'gameloftQA3@gameloft.com':'1111',
            # 'master':'tango',
        }
        self.rights = {'admin':[
            'nikolay.yakovlev@gameloft.com', 
            'konstantin.teslenko@gameloft.com',
            'andrey.sidorov@gameloft.com', 
            'alexandr.soor@gameloft.com',
            'boris.ivakha@gameloft.com',
            'boris.kozar@gameloft.com',
            'denis.belyaev@gameloft.com',
            'roman.yatsenko@gameloft.com',
            'roman.drovikov@gameloft.com',
            'vitaliy.shpotov@gameloft.com',
            'stanislav.podgorskiy@gameloft.com',
            'vadim.kravchenko@gameloft.com',
            'alexey.nemets@gameloft.com',
            'yuriy.boltyan@gameloft.com',
            'bogdan.klochko@gameloft.com',
            ], 'moderator':[], 
            'user':['test1', 'test2', 'test3', 'gameloftQA1@gameloft.com', 'gameloftQA2@gameloft.com', 'gameloftQA3@gameloft.com']}

    def logout(self):
        self.sess[self.session_key] = False

    def auth(self, username, password):
        # if username in self.users and self.users[username] == password:
        #     self.sess[self.session_key] = True
        #     self.sess['username'] = username


        #     self.sess[self.session_key + '_admin'] = False
        #     self.sess[self.session_key + '_moder'] = False
        #     self.sess[self.session_key + '_user'] = False

        #     if username in self.rights['admin']:
        #         self.sess[self.session_key + '_admin'] = True
        #     elif username in self.rights['moderator']:
        #         self.sess[self.session_key + '_moder'] = True
        #     elif username in self.rights['user']:
        #         self.sess[self.session_key + '_user'] = True

        #     return True
        username = "%s" % username.lower()

        if self.ldap(username, password) and username in self.users:
            self.sess[self.session_key] = True
            self.sess['username'] = username

            if username in self.rights['admin']:
                self.sess[self.session_key + '_admin'] = True
            elif username in self.rights['moderator']:
                self.sess[self.session_key + '_moder'] = True
            elif username in self.rights['user']:
                self.sess[self.session_key + '_user'] = True

            return True



        self.sess[self.session_key] = False
        return False

    def ldap(self, username=None, password=None):
        con = ldap.initialize('ldap://kha-dc01.gameloft.org')
        try:
            data = con.simple_bind_s(username, password)
            return True
        except:
            return False

    def get_name(self):
        return self.sess['username']

    def is_admin(self):
        try:
            return self.sess[self.session_key + '_admin']
        except KeyError:
            return False

    def is_moder(self):
        try:
            return self.sess[self.session_key + '_moder']
        except KeyError:
            return False

    def is_user(self):
        try:
            return self.sess[self.session_key + '_user']
        except KeyError:
            return False


    def is_auth(self):
        try:
            return self.sess[self.session_key]
        except KeyError:
            self.sess[self.session_key] = False
            return False
